package com.otherTest.java8.collect;





import cn.hutool.core.collection.CollUtil;
import com.google.common.collect.Maps;
import lombok.Data;
import org.junit.Test;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.Month;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @author conceit
 * @date 2019/1/3 15:28
 */
public class TestCollect {


    /**
     * list转换为treemap
     * 每次循环新生产treemap,消费完毕使用putAll进行合并
     * d为key class为TreeMap.class   泛型<Long,Employee>
     * l是value   是我们要使用的对象  class为Employee.class
     */
    @Test
    public void test1() {
        TreeMap< BigDecimal,Employee> treeMap=persons().stream().collect(Maps::newTreeMap,(d, l)->{
            BigDecimal id=BigDecimal.ZERO;

            if(CollUtil.isNotEmpty(d)){
                System.out.println("key 不为空");
                id=d.lastKey();
            }
            d.put(id.add(l.id),l);
            System.out.println("d-->"+d);
            System.out.println("l-->"+l);
            System.out.println("id-->"+id);
            System.out.println("////////////////////////////////////////////////////////////////////");
        },TreeMap::putAll);
        System.out.println(treeMap);
        System.out.println("treeMap.lastKey()-->"+treeMap.lastKey());
        System.out.println("归集结束--------------------------------------------------------------------");


        Employee employee=null;
        //ThreadLocalRandom.current().nextLong(treeMap.lastKey()),false)
        //返回伪随机均匀分布的值在0(含)和指定值(不包括)之间的长整数值。
        Double random=ThreadLocalRandom.current().nextDouble(treeMap.lastKey().doubleValue());
        //返回比指定key大的map,不包含自己
        //treeMap.tailMap()
        SortedMap<BigDecimal,Employee> sortedMap=treeMap.tailMap(BigDecimal.valueOf(random),false);
        employee=((NavigableMap<BigDecimal, Employee>) sortedMap).firstEntry().getValue();

//        Employee employee=treeMap.tailMap(ThreadLocalRandom.current()
//                .nextLong(treeMap.lastKey()),false).firstEntry().getValue();


        System.out.println("最终结果---");
        System.out.println(employee);


    }
    @Test
    public void test2(){
        long  random=ThreadLocalRandom.current().nextLong(100);
        System.out.println(random);
    }

    public static List<TestCollect.Employee> persons() {
        TestCollect.Employee p1 = new TestCollect.Employee(new BigDecimal("0.1"), "Jake", TestCollect.Employee.Gender.MALE, LocalDate.of(1971,
                Month.JANUARY, 1), 2343.0, new BigDecimal(10));
        TestCollect.Employee p2 = new TestCollect.Employee(new BigDecimal("20.00"), "Jack", TestCollect.Employee.Gender.MALE, LocalDate.of(1972,
                Month.JULY, 21), 7100.0, new BigDecimal(9));
        TestCollect.Employee p3 = new TestCollect.Employee(new BigDecimal("9.9"), "Jane", TestCollect.Employee.Gender.FEMALE, LocalDate.of(1973,
                Month.MAY, 29), 5455.0, new BigDecimal(99));
        TestCollect.Employee p4 = new TestCollect.Employee(new BigDecimal("15.00"), "Jode", TestCollect.Employee.Gender.MALE, LocalDate.of(1974,
                Month.OCTOBER, 16), 1800.0, new BigDecimal(8.888));
        TestCollect.Employee p5 = new TestCollect.Employee(new BigDecimal("25.00"), "Jeny", TestCollect.Employee.Gender.FEMALE, LocalDate.of(1975,
                Month.DECEMBER, 13), 1234.0, new BigDecimal(13.33));
        TestCollect.Employee p6 = new TestCollect.Employee(new BigDecimal("30"), "Jason", TestCollect.Employee.Gender.MALE, LocalDate.of(1976,
                Month.JUNE, 9), 3211.0, new BigDecimal(11.11));

        List<TestCollect.Employee> persons = Arrays.asList(p1, p2, p3, p4, p5, p6);

        return persons;
    }

    @Data
    static class Employee {

        public enum Gender {
            MALE, FEMALE
        }


        private BigDecimal id;
        private String name;
        private TestCollect.Employee.Gender gender;
        private LocalDate dob;
        private double income;
        private BigDecimal amount;

        public Employee(BigDecimal id, String name, TestCollect.Employee.Gender gender, LocalDate dob,
                        double income, BigDecimal amount) {
            this.id = id;
            this.name = name;
            this.gender = gender;
            this.dob = dob;
            this.income = income;
            this.amount = amount;
        }

        public BigDecimal getId() {
            return id;
        }

        public String getName() {
            return name;
        }


    }

}
